package com.lianniu.mgr.product.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.fileupload.FileUpload;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.alibaba.fastjson.JSON;
import com.lianniu.mgr.base.BaseResult;
import com.lianniu.mgr.pagination.PaginationInfo;
import com.lianniu.mgr.product.model.ProductCateVo;
import com.lianniu.mgr.product.model.ProductModelVo;
import com.lianniu.mgr.product.model.ProductPicVo;
import com.lianniu.mgr.product.model.ProductVo;
import com.lianniu.mgr.product.service.IProductCateService;
import com.lianniu.mgr.product.service.IProductPicService;
import com.lianniu.mgr.util.FileUploadUtil;
import com.lianniu.mgr.util.StringUtil;

@Controller
@RequestMapping("/rest/ProductPic")
public class ProductPicRestController {

	private final static Logger logger = Logger.getLogger(ProductModelRestController.class);
	
	@Autowired
	private IProductPicService ProductPicService;
	
	  @Autowired
	  FileUploadUtil fileUpload;
	  
	  @Autowired
	  IProductCateService productCateService;
	  
	  @RequestMapping(value="/ProductPicList",method={RequestMethod.GET,RequestMethod.POST})
	  public @ResponseBody BaseResult ProductPicList(HttpServletRequest request, ProductPicVo curVo, PaginationInfo paginationInfo)
	  {
	       logger.info("ProductPicRestController  ProductPicList begin.");
	       try {
	           
	           logger.info("ProductPicList begin.");          
	           BaseResult baseResult = ProductPicService.selectByProductPicForPage(curVo, paginationInfo);
	           logger.info("ProductPicList end.");
	           logger.info("ProductPicRestController ProductPicList end.");
	           return baseResult;
	       } catch (Exception e) {
	       logger.error("ProductPicRestController ProductPicList error:" + e.getMessage());
	       logger.error("ProductPicRestController ProductPicList end.");
	       return BaseResult.exception(e.getMessage());
	     }
	  }
	  
	
	  @RequestMapping(value = {"/ProductPicAdd"}, method = RequestMethod.POST)
	  public @ResponseBody BaseResult ProductPicAdd(HttpServletRequest request,ProductPicVo curProductPicVo) {
		  String cateId = request.getParameter("cateName");
		  curProductPicVo.setCateId(cateId);
	      logger.info("ProductPicRestController ProductPicAdd begin.");      
	      logger.info("ProductPicRestController ProductPicAdd:"+ JSON.toJSONString(curProductPicVo));
	      try {
	          logger.info("ProductPicAdd begin.");
	          BaseResult baseResult = ProductPicService.insertProductPic(curProductPicVo);
	          logger.info("ProductPicAdd baseResult:" + JSON.toJSONString(baseResult));
	          logger.info("ProductPicAdd end.");
	          return baseResult;

	    } catch (Exception e) {
	      logger.error("ProductAdd error:"+e.getMessage());
	      logger.info("ProductPicRestController ProductAdd end.");
	      return BaseResult.exception(e.getMessage());
	    }

	  } 
	
	@RequestMapping(value = {"/ProductPicEdit"}, method = RequestMethod.POST)
	  public @ResponseBody BaseResult ProductPicEdit(HttpServletRequest request,ProductPicVo curVo) {
	      logger.info("ProductPicRestController ProductPicEdit begin.");      
	      logger.info("ProductPicRestController ProductPicEdit:"+ JSON.toJSONString(curVo));
	      try {
	          logger.info("ProductPicEdit begin.");
	          BaseResult baseResult = ProductPicService.updateById(curVo);
	          logger.info("ProductPicEdit baseResult:" + JSON.toJSONString(baseResult));
	          logger.info("ProductPicEdit end.");
	          logger.info("picId:"+JSON.toJSONString(request.getParameter("picId")));
	          return baseResult;

	    } catch (Exception e) {
	      logger.error("ProductPicEdit error:"+e.getMessage());
	      logger.info("ProductPicRestController ProductPicEdit end.");
	      return BaseResult.exception(e.getMessage());
	    }
	  }  
	
	@SuppressWarnings("unchecked")
	@RequestMapping(value="/selectProductPicById",method={RequestMethod.GET,RequestMethod.POST})
	  public @ResponseBody BaseResult selectProductPicById(HttpServletRequest request,ProductPicVo curVo) {
	        logger.info("ProductPicRestController selectProductPicById begin.");
	        try {
	                    
	          BaseResult baseResult = ProductPicService.selectById(curVo);
	          ProductPicVo pcv = (ProductPicVo)baseResult.getData();
	          List<ProductCateVo> cateParentNames = (List<ProductCateVo>)productCateService.getByParentId("0").getData();
	          List<ProductCateVo> cateChildNames = (List<ProductCateVo>)productCateService.getByParentId(pcv.getCateChildId()).getData();
	          List<ProductCateVo> brandParentNames = (List<ProductCateVo>)productCateService.getByParentId(pcv.getBrandParentId()).getData();
	          List<ProductCateVo> brandChildNames = (List<ProductCateVo>)productCateService.getByParentId(pcv.getBrandChildId()).getData();
	          List<ProductCateVo> cateNames = (List<ProductCateVo>)productCateService.getByParentId(pcv.getBrandChildId()).getData();
	          Map<String,Object> map=new HashMap<String,Object>();
	          
	          map.put("cateParentNames", cateParentNames);
	          map.put("cateChildNames", cateChildNames);
	          map.put("brandParentNames", brandParentNames);
	          map.put("brandChildNames", brandChildNames);
	          map.put("cateNames", cateNames);
	          map.put("pcv", pcv);
	          
	          logger.info("selectDictionaryById end.");
	          logger.info("ProductRestController selectProductById end.");
	          return BaseResult.success(map);
	        } catch (Exception e) {
	          logger.error("ProductPicRestController selectProductPicById error:" + e.getMessage());
	          logger.error("ProductPicRestController selectProductPicById end.");
	          return BaseResult.exception(e.getMessage());
	        }
	  } 
	@RequestMapping(value = {"/ProductPicDel"}, method = RequestMethod.POST)
	  public @ResponseBody BaseResult ProductPicDel(HttpServletRequest request) {
	      logger.info("ProductPicRestController ProductPicDel begin.");
	     try {          
	          logger.info("ProductPicDel begin.");
	          BaseResult baseResult = ProductPicService.deleteProductPic(request.getParameter("ids").split(","));	        
	          logger.info("ProductPicDel baseResult:" + JSON.toJSONString(baseResult));
	          logger.info("ProductPicDel end.");
	          
	          logger.info("ProductPicRestController ProductPicDel end.");
	          return baseResult;

	    } catch (Exception e) {
	      logger.error("ProductPicRestController ProductPicDel error:" + e.getMessage());
	      logger.info("ProductPicRestController ProductPicDel end.");
	      return BaseResult.exception(e.getMessage());
	    }

	  }

	@RequestMapping(value = {"/checkPicName"}, method = RequestMethod.POST)
	  public @ResponseBody Boolean checkPicName(HttpServletRequest request) {
	      Boolean flag=true;
	      String picName=  request.getParameter("picName");
	      logger.info("ProductPicRestController checkPicName begin.");
	     try {          
	          logger.info("checkPicName begin.");
	          if("".equalsIgnoreCase(picName)||picName==null)
	          {return flag;}
	          flag = ProductPicService.checkPicName(picName);         
	          logger.info("checkPicName end.");
	          
	          logger.info("ProductPicRestController checkPicName end.");
	          return flag;
	    } catch (Exception e) {
	      logger.error("ProductPicRestController checkPicName error:" + e.getMessage());
	      logger.info("ProductPicRestController checkPicName end.");
	      return flag;
	    }

	  }
	@RequestMapping(value = {"/uploadPic"}, method = {  RequestMethod.POST })
	  public @ResponseBody BaseResult uploadPic(HttpServletRequest request) throws Exception {
	      logger.info("ProductPicRestController add begin.");
	      MultipartHttpServletRequest re = (MultipartHttpServletRequest) request;
	      MultipartFile file = re.getFile(request.getParameter("id"));
	      if(file != null){
	        String path = fileUpload.getPath(file, request, "/productPic/");
	        return BaseResult.success(path);
	      }
	      return BaseResult.success();
	  }
	
	 @RequestMapping(value = {"/checkPicNameEdit"}, method = RequestMethod.POST)
	  public @ResponseBody Boolean checkPicNameEdit(HttpServletRequest request) {
	      Boolean flag=true;
	      String picName=  request.getParameter("picName");
	      String picId=request.getParameter("picId");
	      logger.info("ProductPicRestController checkPicNameEdit begin.");
	     try {          
	          logger.info("checkPicNameEdit begin.");
	          ProductPicVo vo=new ProductPicVo();	         
	          vo.setPicId(picId);
	          
	          BaseResult baseResult =ProductPicService.selectById(vo);	          
	          ProductPicVo curVo=(ProductPicVo) baseResult.getData();
	          if(curVo.getPicId().equals(picId)&&curVo.getPicName().equals(picName))
	          {return flag;}
	          if("".equalsIgnoreCase(picName)||picName==null)
	          {return flag;}
	          flag = ProductPicService.checkPicName(picName);         
	          logger.info("checkPicNameEdit end.");
	          
	          logger.info("ProductPicRestController checkPicNameEdit end.");
	          return flag;
	    } catch (Exception e) {
	      logger.error("ProductPicRestController checkPicNameEdit error:" + e.getMessage());
	      logger.info("ProductPicRestController checkPicNameEdit end.");
	      return flag;
	    }
	     
	     }
	 @RequestMapping(value="/getByCate",method={RequestMethod.POST,RequestMethod.GET})
	 public @ResponseBody BaseResult getByCate(HttpServletRequest request){
		 String cateId=request.getParameter("cateId");
		 logger.info("ProductPicRestController getByCate begin.");
		 try{
			 logger.info("getByCate begin.");
			 BaseResult baseResult=ProductPicService.getByCateId(cateId);
			 logger.info("getByCate end.");
			 logger.info("ProductPicRestController getByCate end.");
			 return baseResult;
		 }catch(Exception e){
			 logger.error("queryByParentId error:"+e.getMessage());
			 logger.info("ProductPicRestController getByCate end.");
			 return BaseResult.exception(e.getMessage());
		 }

	  }
	 @RequestMapping(value="/getByPrentId",method={RequestMethod.POST,RequestMethod.GET})
	 public @ResponseBody BaseResult getByPrentId(HttpServletRequest request,ProductCateVo curVo){
		 logger.info("ProductPicRestController getByPrentId begin.");
		 try{
			 logger.info("getByPrentId begin.");
			 BaseResult baseResult=ProductPicService.getByPrentId("prentId");
			 logger.info("getByPrentId end.");
			 logger.info("ProductPicRestController getByPrentId end.");
			 return baseResult;
		 }catch(Exception e){
			 logger.error("getByPrentId error:"+e.getMessage());
			 logger.info("ProductPicRestController getByPrentId end.");
			 return BaseResult.exception(e.getMessage());
		 }

	  }
	
}
